<template>
	<view>
		<!-- 搜索框 -->
		<my-search bgcolor="#ff00ff" radius="10" @click="gotoSearch"></my-search>
		<!-- 分类选择 -->
		<view class="cate-container">
			<!-- 左 一级 -->
			<scroll-view class="cate-left" scroll-y :style="{height: wh + 'px'}">
				<view :class="['cate-left-item', active === i? 'active': '']" v-for="(item, i) in cateList" :key="i" @click="changeActive(i)" >{{item.cat_name}}</view>
			</scroll-view>
			<!-- 右 二级 -->
			<scroll-view class="cate-right" scroll-y :style="{height: wh+'px'}" :scroll-top="scrollTop">
				<view class="cate-right-item" v-for="(item2, i2) in cateLevelTwo" :key="i2">
					<!-- 标题 -->
					<view class="cate-right-item-title">/{{ item2.cat_name }}/</view>
					
					<!-- 内容 -->
					<view class="cate-right-item-content">
						<view class="cate-right-item-content-box"  v-for="(item3, i3) in item2.children" :key="i3" @click="goGoodsList(item3)">
							<image :src="item3.cat_icon" mode="widthFix"></image>
							<text>{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		// 将 badgeMix 混入到当前的页面中进行使用
		mixins: [badgeMix],
		data() {
			return {
				// 全部数据
				cateList: [],
				// 窗口可用高度
				wh: 0,
				// 一级选中判断
				active: 0,
				// 二级分类
				cateLevelTwo: [],
				// 滚动条距离顶部的距离
				scrollTop: 0
			};
		},
		onLoad() {
			// 获取页面可用高度
			const sysInfo = uni.getSystemInfoSync()
			// wh赋值
			this.wh = sysInfo.windowHeight - 50
			// 获取全部数据
			this.getCateList()
			
		},
		methods: {
			// 获取全部数据
			async getCateList() {
				const {data: res} = await uni.$http.get('/categories')
				if(res.meta.status !== 200) return $http.showMsg()
				this.cateList = res.message
				this.cateLevelTwo = res.message[0].children
			},
			// 一级 active 
			changeActive(i) {
				// console.log(i)
				this.active = i
				this.cateLevelTwo = this.cateList[i].children
				this.scrollTop = Math.random()
			},
			// 点击 二级 跳转
			goGoodsList(cate) {
				uni.navigateTo({
					url: `/subpkg/goods_list/goods_list?cid=${cate.cat_id}`
				})
			},
			// 点击搜索
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
				// console.log('父组件')
			}
		}
	}
</script>

<style lang="scss">
.cate-container {
		display: flex;

		.cate-left {
			width: 240rpx;

			.cate-left-item {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 120rpx;
				background-color: #efefef;
				font-size: 24rpx;

				&.active {
					display: flex;
					justify-content: space-between;
					align-items: center;
					background-color: #ffffff;

					// 左边红线
					&::before {
						content: '';
						width: 6rpx;
						height: 60rpx;
						background-color: #c00000;
					}

					// 右边白线和背景相同
					&::after {
						content: '';
						width: 6rpx;
						height: 60rpx;
						// background-color: #c00000;
						background-color: #fffff;
					}
				}
			}
		}

		.cate-right {
			.cate-right-item {
				.cate-right-item-title {
					// 这里的样式不是确定
					display: flex;
					justify-content: center;
					font-weight: bold;
					font-size: 24rpx;
					padding: 20rpx 0;
				}

				.cate-right-item-content {
					display: flex;
					flex-wrap: wrap;

					.cate-right-item-content-box {
						width: 33.33%;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						width: 33.33%;
						padding: 20rpx;
						box-sizing: border-box;

						image {
							width: 100%;
						}

						text {
							font-size: 24rpx;
						}
					}
				}
			}
		}
	}
</style>
